<?php
include "inc/options.php";
include "inc/il8n.php";
if (isset($_REQUEST['PHPSESSID'])) session_start($_REQUEST['PHPSESSID']); else session_start();
if ($_GET['lang']) {
	$_SESSION['lang']=$_GET['lang'];
	$lang=$_GET['lang'];
}
if ($_SESSION['lang']) {
	$lang=$_SESSION['lang'];
}
if (!$lang) {
	$lang="en";
}
if (($lang!="en") && ($lang!="fr")) {
	$lang="en";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<meta name="keywords" content="tm,tmn,track mania, trackmania nation" />
		<meta name="description" content="Track Mania Nations Tools : des outils pour TMN" />
		<link rel="stylesheet" type="text/css" href="style.css" media="screen"/>
		<script type="text/javascript" src="inc/tm.js"></script>
		<title><?php echo $il8n[$lang]["title"]; ?></title>

	</head>
	<body>
		<div id="Main">
			<div id="Header"><a href="index.php"><?php echo $il8n[$lang]["title"]; ?></a></div>
			<div id="HeaderMenus">
				<ul>
					<li><a title="<?php echo $il8n[$lang]["home"]; ?>" href="index.php"><?php echo $il8n[$lang]["home"]; ?></a></li>	
					<li><a title="<?php echo $il8n[$lang]["my"]; ?>" href="index.php?p=login"><?php echo $il8n[$lang]["my"]; ?> <? if ($_SESSION['user']) { echo "(".$_SESSION['user'].")"; } ?></a></li>
					<? if ($_SESSION['user']) { ?> <li><a title="<?php echo $il8n[$lang]["option"]; ?>" href="index.php?p=option"><?php echo $il8n[$lang]["option"]; ?></a></li> <?php } ?>
				</ul>
			</div>
			<div id="SideContents">
				<h3>Login</h3>
				<?php if (!$_SESSION['user']) { ?>
				<form action="index.php?p=login&act=login" method="POST">
					<input type="text" name="user" size="15" value="Username" onfocus="if(this.value=='Username')this.value=''"/><br />
					<input type="password" name="pass" size="15" value="Password" onfocus="if(this.value=='Password')this.value=''"/><br />
					<input type="submit" value="S'identifier" />
				</form>
				<?php } else { ?>
				<?php echo $il8n[$lang]["connect1"].$_SESSION['user'].$il8n[$lang]["connect2"]; ?> <br /><a href="index.php?p=logout"><?php echo $il8n[$lang]["logout"]; ?></a>
				<?php } ?>
				<h3><?php echo $il8n[$lang]["lang"]; ?></h3>
				<a href="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>&lang=fr"><img src="flags/FRA.png" class="flag" /></a> <a href="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>&lang=en"><img src="flags/GBR.png" class="flag" /></a> 
			</div>
			<div id="MainContent">
					<?php if (!$_GET['p']) { ?>

					<p><?php echo $il8n[$lang]["main"]; ?></p>
				<?php } ?>

				<?php if (($_GET['p'] == "login") && (!$_GET['act']) && ($_SESSION['ok'])) { ?>
					<?
					mysql_connect(HOST,USER,PASS);
					mysql_select_db(BDD);
					$result=mysql_query("SELECT * FROM `$table_users` WHERE user='".$_SESSION['user']."'");
					$n=mysql_num_rows($result);
					if ($n==0) {
						// Si on ne trouve pas d'infos sur cet utilisateur, on détruit la session ;
						// session_destroy();
						echo "<h2>Error</h2>";
					}
					else {
						// Everything should be OK :P
						$temp=mysql_fetch_array($result);
						if (empty($temp['steamid'])) { ?>
							<h2><?php echo $il8n[$lang]["my"]; ?></h2><p><?php echo $il8n[$lang]["steam"]; ?></p>
							<p><?php echo $il8n[$lang]["steamwarn"]; ?></p>
							
							<p>
								<form action="index.php?p=login&act=reg" method="POST" name="register" onsubmit="return confirmlogin(document.forms['register'].elements['login'].value)">
									<input type="text" name="login" />
									<input type="submit" value="<?php echo $il8n[$lang]["enregistrer"]; ?>"/>
								</form>
							</p>
						<? }
						else { ?>
						<h2><?php echo $il8n[$lang]["my"]; ?></h2><p><?php echo $il8n[$lang]["steamregistered"]; ?> <b><? echo $temp['steamid']; ?></b></p>
						<h2><?php echo $il8n[$lang]["cache"]; ?></h2>
						<?php 
							$result2=mysql_query("SELECT * FROM `$table_scores` WHERE name='".$_SESSION['user']."'") or die(mysql_error());
							$n2=mysql_num_rows($result2);
							if ($n2==0) {
								echo "<p>".$il8n[$lang]["nothing"]."</p>";
							}
							else {
								$temp=mysql_fetch_array($result2);
								print $il8n[$lang]["cacheNAME"].$temp["name"]."<br />";
								print $il8n[$lang]["cacheRANK"].$temp["rank"]."<br />";
								print $il8n[$lang]["cacheTIME"].$temp["time"]."<br />";
								print $il8n[$lang]["cachePOINTS"].$temp["points"]."<br />";
								print $il8n[$lang]["cacheFRAGS"].$temp["frags"]."<br />";
								print $il8n[$lang]["cacheDEATHS"].$temp["deaths"]."<br />";
								print $il8n[$lang]["cacheRATIO"].$temp["ratio"]."<br /><br />";
								print $il8n[$lang]["cacheDATE"].date("d/m/y H:i",$temp["date"])."<br /><br />";
							}
						?>
						<h2><?php echo $il8n[$lang]["button"]; ?></h2>
						<p><form><?php for ($i=1;$i<3;$i++) {
							print "<img src=\"img-".$_SESSION['user']."-$i.jpg\" /><br />";
							print "<table>";
							print "<tr><td>".$il8n[$lang]["link"]."</td><td><input type=\"text\" name=\"input1$i\" size=\"50\" value=\"$site_url/img-".$_SESSION['user']."-$i.jpg\" onclick=\"this.form.input1$i.select();\" /></td></tr>";
							$link=htmlentities("<a href=\"$site_url\"><img src=\"$site_url/img-".$_SESSION['user']."-$i.jpg\" /></a>");
							print "<tr><td>".$il8n[$lang]["link2"]."</td><td><input type=\"text\" name=\"input2$i\" size=\"50\" value=\"$link\" onclick=\"this.form.input2$i.select();\" /></td></tr>";
							$link=htmlentities("[URL=$site_url][IMG]$site_url/img-".$_SESSION['user']."-$i.jpg[/IMG][/URL]");
							print "<tr><td>".$il8n[$lang]["link3"]."</td><td><input type=\"text\" name=\"input3$i\" size=\"50\" value=\"$link\" onclick=\"this.form.input3$i.select();\" /></td></tr>";
							print "</table><hr />";
						}
                                                print "</p></form>";
						}
						}
					?>
				<?php } ?>
				<?php if (($_GET['p'] == "login") && ($_GET['act'] == reg) && ($_SESSION['ok'])) { ?>
					<?
					$login=addslashes(htmlentities($_POST['login']));
					if (empty($login)) { echo "<h2>".$il8n[$lang]["erreur"]."</h2><p>".$il8n[$lang]["erreur3"]."</p>"; }
					mysql_connect(HOST,USER,PASS);
					mysql_select_db(BDD);
					// On change le login
					$result=mysql_query("UPDATE `$table_users` SET steamid='$login' WHERE user='".$_SESSION['user']."'");
					echo "<h2>".$il8n[$lang]["modif"]."</h2><p>".$il8n[$lang]["modifOK"]."</p>";
					?>
				<?php } ?>
				<?php if (($_GET['p'] == "login") && (!$_GET['act']) && (!$_SESSION['ok'])) { ?>
					<p><?php echo $il8n[$lang]["register"]; ?></p>
					<p><?php echo $il8n[$lang]["question"]; ?> <a href="index.php?p=login&act=login"><?php echo $il8n[$lang]["clic"]; ?></a> <?php echo $il8n[$lang]["clic2"]; ?></p>
					<p<a href="index.php?p=login&act=register"><?php echo $il8n[$lang]["clic"]; ?></a> <?php echo $il8n[$lang]["clic3"]; ?></p>
				<?php } ?>
				<?php if (($_GET['p'] == "login") && ($_GET['act'] == "register") && (!$_POST) && (!$_SESSION['ok'])) { ?>
					<h2><?php echo $il8n[$lang]["register"]; ?></h2>
					<p><?php echo $il8n[$lang]["time"]; ?><br />
						<form action="index.php?p=login&act=register" method="POST">
							<label><input type="text" name="user" size="10" /> : Username</label><br />
							<label><input type="password" name="pass" size="10" /> : Pass</label><br />
							<label><input type="password" name="pass2" size="10" /> : Pass </label><br />
							<input type="Submit" value="<?php echo $il8n[$lang]["sinscrire"]; ?>" />
						</form>
					</p>
				<?php } ?>
				<?php if (($_GET['p'] == "login") && ($_GET['act'] == "register") && ($_POST) && (!$_SESSION['ok'])) { ?>
				<?php 
					// On récupère / nettoi toute les informations
					$username=addslashes(htmlentities($_POST['user']));
					$pass=addslashes(htmlentities($_POST['pass']));
					$pass2=addslashes(htmlentities($_POST['pass2']));
					
					// Si il manque un champ, on arrête
					if ((empty($username)) || (empty($pass)) || (empty($pass2)) ) {
						echo "<h2>".$il8n[$lang]["erreur"]."</h2><p><b>".$il8n[$lang]["erreur1"]."</b></p>";
						print "<p><i>".$il8n[$lang]["autoredirect"]." <a href=\"$site_url/index.php?p=login&act=register\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p><script type=\"text/javascript\">window.setTimeout(\"location=('$site_url/index.php?p=login&act=register');\",5000)</script>";
						$error=True;
					}
					
					//Si les deux pass ne sont pas identiques
					elseif ($pass != $pass2) {
						echo "<h2>".$il8n[$lang]["erreur"]."</h2><p><b>".$il8n[$lang]["erreur2"]."</b></p>";
						print "<p><i>".$il8n[$lang]["autoredirect"]." <a href=\"$site_url/index.php?p=login&act=register\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p><script type=\"text/javascript\">window.setTimeout(\"location=('$site_url/index.php?p=login&act=register');\",5000)</script>";
						$error=True;
					}
					elseif (!preg_match("/^[a-z0-9A-Z]+$/",$username)) {
						echo "<h2>".$il8n[$lang]["erreur"]."</h2><p><b>".$il8n[$lang]["erreur4"]."</b></p>";
						print "<p><i>".$il8n[$lang]["autoredirect"]." <a href=\"$site_url/index.php?p=login&act=register\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p><script type=\"text/javascript\">window.setTimeout(\"location=('$site_url/index.php?p=login&act=register');\",5000)</script>";
						$error=True;
					}
					if (!$error) {
					// On vérifie que l'username est pas pris.
					mysql_connect(HOST,USER,PASS);
					mysql_select_db(BDD);
					$result=mysql_query("SELECT * FROM `$table_users` WHERE user='$username'") or die(mysql_error());
					$n=mysql_num_rows($result);
					if ($n == 0) { 
						$q=mysql_query("INSERT INTO `$table_users` (user,pass) VALUES ('$username', '$pass2')") or die(mysql_error());
						if ($q) { 
							echo $il8n[$lang]["registerok"];
							print "<p><i>".$il8n[$lang]["autoredirect"]." <a href=\"$site_url/index.php?p=login\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p><script type=\"text/javascript\">window.setTimeout(\"location=('$site_url/index.php?p=login');\",5000)</script>";
							// On écrit quelques variables de session pour s'en souvenir ;P
							$_SESSION['user']=$username;
							$_SESSION['ok']="ok";
						}
						else {
							echo "Error";
						}
					}
					
					else {
						echo "<h2>".$il8n[$lang]["erreur"]."</h2><p>".$il8n[$lang]["taken"]."</p>";
						print "<p><i>".$il8n[$lang]["autoredirect"]." <a href=\"$site_url/index.php?p=login&act=register\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p><script type=\"text/javascript\">window.setTimeout(\"location=('$site_url/index.php?p=login&act=register');\",5000)</script>";
					}
					}
				?>
				<?php } ?>
				<?php if (($_GET['p'] == "login") && ($_GET['act'] == "login") && (!$_POST) && (!$_SESSION['ok'])) { ?>
					<h2><?php echo $il8n[$lang]["login"]; ?></h2>
					<p><?php echo $il8n[$lang]["login2"]; ?><br />
						<form action="index.php?p=login&act=login" method="POST">
							<label><input type="text" name="user" size="15" /> : Username</label><br />
							<label><input type="password" name="pass" size="15" /> : Pass</label><br />
							<input type="submit" value="<?php echo $il8n[$lang]["login"]; ?>" />
						</form>
					</p>
				<?php } ?>
				<?php if (($_GET['p'] == "login") && ($_GET['act'] == "login") && ($_POST) && (!$_SESSION['ok'])) { ?>
					<?
					// On récupère/nettoie les variables
					$username=addslashes(htmlentities($_POST['user']));
					$pass2=addslashes(htmlentities($_POST['pass']));
					// On vérifie le login..
					mysql_connect(HOST,USER,PASS);
					mysql_select_db(BDD);
					$result=mysql_query("SELECT * FROM `$table_users` WHERE user='$username'") or die(mysql_error());
					// On ferme la connexion
					mysql_close();
					$n=mysql_num_rows($result);
					if ($n == 0) {
						echo "<h2>".$il8n[$lang]["erreur"]."</h2><p>".$il8n[$lang]["empty"]."</p>";
					}
					else {
						$temp=mysql_fetch_array($result);
						if ($temp['pass']!=$pass2) {
							echo "<h2>".$il8n[$lang]["erreur"]."</h2><p>".$il8n[$lang]["pass"]."</p>";
						}
						else {
							$_SESSION['user']=$temp['user'];
							$_SESSION['ok']="ok";
							echo "<h2>".$il8n[$lang]["login"]."</h2><p>".$il8n[$lang]["ok"]."</p>";
							print "<p><i>".$il8n[$lang]["autoredirect"]." <a href=\"$site_url/index.php?p=login\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p><script type=\"text/javascript\">window.setTimeout(\"location=('$site_url/index.php?p=login');\",5000)</script>";
						}
					}
					?>
				<?php } ?>
				<?php if (($_GET['p'] == "option") && (!$_POST) && (!$_GET['act'])) { ?>
					<h2><?php echo $il8n[$lang]["option"]; ?></h2>
					<?php
						mysql_connect(HOST,USER,PASS);
						mysql_select_db(BDD);
						$result=mysql_query("SELECT display FROM `$table_users` WHERE user='".$_SESSION['user']."'") or die(mysql_error());
						$n=mysql_num_rows($result);
						if ($n>0) { $temp=mysql_fetch_array($result); }
					?>
						<form action="index.php?p=option" method="POST">
							<label><input type="text" name="display" size="15" value="<?php echo $temp['display']; ?>"/> : <?php echo $il8n[$lang]["display"]; ?></label><br />
							<input type="submit" value="<?php echo $il8n[$lang]["enregistrer"]; ?>" />
						</form>
				<?php } ?>
				<?php if (($_GET['p'] == "option") && (!$_POST) && ($_GET['act'] == "delcache")) { ?>
					<h2><?php echo $il8n[$lang]["option"]; ?></h2>
                                            <p><?php echo $il8n[$lang]["cached"]; ?></p>
                                            
				<?php } ?>
				<?php if (($_GET['p'] == "option") && ($_POST)) { ?>
					<h2><?php echo $il8n[$lang]["option"]; ?></h2>
					<?php 
						$display=addslashes($_POST["display"]);
						if (strlen($display) > 25) {
							print "<p>".$il8n[$lang]["displayerror"]."</p>";
						}
						else {
							mysql_connect(HOST,USER,PASS);
							mysql_select_db(BDD);
							$result=mysql_query("UPDATE $table_users SET display='$display' WHERE user='".$_SESSION['user']."'");
							print "<p>".$il8n[$lang]["displayOK"]."</p>";
						}
					?>
				<?php } ?>
				<?php if ($_GET['p'] == "logout") { ?>
				<?php session_destroy(); ?>
				<h2><?php echo $il8n[$lang]["deco"]; ?></h2>
				<p><?php echo $il8n[$lang]["decoOK"]; ?>.</p>
				<p><i><?php print $il8n[$lang]["autoredirect"]." <a href=\"$site_url\">".$il8n[$lang]["clic"]."</a> ".$il8n[$lang]["autoredirectfalse"]."</i></p>"; ?><script type="text/javascript">window.setTimeout("location=('<?php echo $site_url; ?>');",5000)</script>
				<?php } ?>
				<?php if ($_GET['p'] == "changelog") { ?>
					<h2>Changelog</h2>
					<ul>
						<li>v0.2.3
							<ul>
                                                                <li>Fixed a problem in login</li>
                                                                <li>Automatic select on focus in 'My account' forms</li>
                                                                <li>Added an option to delete image cache</li>
                                                        </ul>
						</li>
						<li>v0.2.2 
							<ul>
                                                                <li>JPEG quality was not 100 in the cache</li>
                                                        </ul>
						</li>
						<li>v0.2.1 
							<ul>
                                                                <li>Added 1 style</li>
							        <li>Now 100 jpeg quality</li>
                                                        </ul>
						</li>
						<li>v0.2 
							<ul>
								<li>Added link to changelog</li>
								<li>Bug corrected in multi language support</li>
								<li>Added an option to customize the display name on the button</li>
								<li>Added automatic redirect on some page</li>
								<li>Fixed bugs on the button</li>
								<li>Added many verifications on login/steamid</li>
								<li>Added an option to not cache the image (?nocach=true), used on My account page</li>
							</ul>
						</li>
						<li>v0.1 : Initial release</li>	
					<ul>
				<?php } ?>
			</div>
			<div id="Footer">
				<a href="?p=changelog">v0.2.3</a> magnetik 2007 <a href="http://www.xiti.com/xiti.asp?s=318554" title="WebAnalytics">
<script type="text/javascript">
<!--
Xt_param = 's=318554&p=';
try {Xt_r = top.document.referrer;}
catch(e) {Xt_r = document.referrer; }
Xt_h = new Date();
Xt_i = '<img width="39" height="25" border="0" alt="" ';
Xt_i += 'src="http://logv143.xiti.com/hit.xiti?'+Xt_param;
Xt_i += '&hl='+Xt_h.getHours()+'x'+Xt_h.getMinutes()+'x'+Xt_h.getSeconds();
if(parseFloat(navigator.appVersion)>=4)
{Xt_s=screen;Xt_i+='&r='+Xt_s.width+'x'+Xt_s.height+'x'+Xt_s.pixelDepth+'x'+Xt_s.colorDepth;}
document.write(Xt_i+'&ref='+Xt_r.replace(/[<>"]/g, '').replace(/&/g, '$')+'" title="Internet Audience">');
//-->
</script>
<noscript>
Mesure d'audience ROI statistique webanalytics par <img width="39" height="25" src="http://logv143.xiti.com/hit.xiti?s=318554&p=" alt="WebAnalytics" />
</noscript></a>
				
			</div>
		</div>
	</body>
</html>